package jeecg.business.thread.task;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import jeecg.business.entity.enter.EnterEntity;
import jeecg.business.entity.keyinfo.KeyinfoEntity;
import jeecg.business.entity.keyword.KeywordEntity;
import jeecg.business.entity.log.CollLogEntity;
import jeecg.business.entity.site.SiteEntity;
import jeecg.business.job.FetchTask;
import jeecg.business.service.keyinfo.KeyinfoServiceI;
import jeecg.business.service.log.CollLogServiceI;
import jeecg.business.service.loginuser.LoginUserServiceI;
import jeecg.business.service.site.SimpleSiteServiceI;
import jeecg.business.util.WordHandlerUtil;
import jeecg.business.vo.ContainWordNum;
import jeecg.business.vo.WordNumComparator;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.http.HttpClientUtil;
/**
 * 
 * <p>
 * 标题: 抽象抓取任务
 * </p>
 * <p>
 * 描述: // TODO
 * </p>
 * 
 * @author LinYingPiaoJian(Q:71426052)
 * @version // TODO
 * @since 2015-6-6 下午2:55:17
 */
public abstract class ExecTask implements Runnable{
	
	/**
	 * 只抓取前三页
	 */
	public int maxpage = 50;
	
	protected KeywordEntity keyword;
	
	protected SiteEntity site;
	
	protected EnterEntity enter;
	
	protected KeyinfoServiceI keyinfoService;
	
	protected CollLogServiceI collLogService;
	
	protected SimpleSiteServiceI simpleSiteService;
	
	protected LoginUserServiceI loginUserService;
	
	public static String KEYWORD_FLAG = "{keyword}";
	
	public static String LONG_STARTTIME_FLAG = "{longstarttime}";
	
	public static String STARTTIME_FLAG = "{starttime}";
	
	public static String ENDTIME_FLAG = "{endtime}";
	
	public static String PAGE_FLAG = "{page}";
	
	public ExecTask(EnterEntity enter , KeywordEntity keyword , SiteEntity site ){
		this.enter = enter;
		this.keyword = keyword;
		this.site = site;
	}

	public abstract void run();
	
	public boolean filter(EnterEntity enter , KeyinfoEntity keyinfo , KeywordEntity keyword){
		String ppnr = keyword.getPpnr();
		String bppgjc = keyword.getBppgjc();
		
		
		String title = keyinfo.getTitle();
		String content = keyinfo.getBodycontent();
		if(title==null) title = "";
		if(content==null) content = "";
		
		/**
		 * 设置屏蔽词
		 */
		if(WordHandlerUtil.hasShieldWord(title, bppgjc)){
			return false;
		}
		
		
		
		/**
		 * 如果没有|，就讲词库中的词都加进去
		 */
		if(!ppnr.contains("|") && "1".equals(keyword.getPpck())){
			ppnr += "+("+FetchTask.getAlarmString()+")";
		}
		
		/**
		 * 匹配内容
		 */
		String [] pps = ppnr.split("\\+");
		List<String> mustlist = new ArrayList<String>();
		List<ContainWordNum> wmlist = new ArrayList<ContainWordNum>();
		List<String> maylist = new ArrayList<String>();
		String [] mays = null;
		for(String pp : pps){
			if(pp.contains("|")){
				pp = pp.replace(")", "").replace("(", "").replace("（", "").replace("）", "");
				mays = pp.split("\\|");
				for(String may : mays)
					maylist.add(may);
			}else{
				mustlist.add(pp);
			}
		}
		
		boolean flag = true;
		for(String must : mustlist)
			flag = flag && (title.contains(must) || content.contains(must));
		
		String mayStr = null;
		ContainWordNum wn = null;
		
		if(!"1".equals(keyword.getPpck())){
			maylist.addAll(mustlist);
		}
		
		for(String may : maylist){
			if(title.contains(may) || content.contains(may)){		//添加次数
				int count = 0;
				count = StringUtil.getStrAppearNum(title, may);
				count += StringUtil.getStrAppearNum(content, may);
				wn = new ContainWordNum();
				wn.setWord(may);
				wn.setNum(count);
				wmlist.add(wn);
			}
		}
		
		Collections.sort(wmlist, new WordNumComparator());
		String refwordnum = null;
		for(ContainWordNum c : wmlist){
			if(mayStr == null){
				mayStr = c.getWord();
				refwordnum = c.getNum()+"";
			}
			else{
				mayStr += "|"+c.getWord();
				refwordnum += "|"+c.getNum();
			}
			
		}
		
		flag = flag && mayStr!=null;
		
		if(flag){
			keyinfo.setRefword(mayStr);
			keyinfo.setRefwordnum(refwordnum);
			keyinfo.setKeyword(keyword.getKeyword());
		}
		
		return flag;
	}
	
	
	
	
	
	public static String getEncodeByHref(String href , String bmlx , String cookies) throws XPatherException{
		/**
		 * 获取编码
		 */
		TagNode node = null;
		TagNode headNode = null;
		HtmlCleaner cleaner = new HtmlCleaner();
		String html = HttpClientUtil.getSourceFromUrl(href, bmlx , cookies);
		node = cleaner.clean(html);
		/**
		 * 判断编码是否相同
		 */
//		headNode = (TagNode)node.evaluateXPath("//head")[0];
		String encode = HttpClientUtil.getEncode(cleaner , node);
		return encode;
	}

	public KeyinfoServiceI getKeyinfoService() {
		return keyinfoService;
	}

	public void setKeyinfoService(KeyinfoServiceI keyinfoService) {
		this.keyinfoService = keyinfoService;
	}

	public CollLogServiceI getCollLogService() {
		return collLogService;
	}

	public void setCollLogService(CollLogServiceI collLogService) {
		this.collLogService = collLogService;
	}
	
	public SimpleSiteServiceI getSimpleSiteService() {
		return simpleSiteService;
	}

	public void setSimpleSiteService(SimpleSiteServiceI simpleSiteService) {
		this.simpleSiteService = simpleSiteService;
	}

	public LoginUserServiceI getLoginUserService() {
		return loginUserService;
	}

	public void setLoginUserService(LoginUserServiceI loginUserService) {
		this.loginUserService = loginUserService;
	}

	protected CollLogEntity saveLog(){
		CollLogEntity log = new CollLogEntity();
		log.setCjzd(site.getMc());
		log.setGjc(keyword.getKeyword());
		log.setKssj(new Date());
		log.setSfcg("1");
		collLogService.save(log);
		return log;
	}
	
	public static void main(String[] args) {
		String s = "北京+(奥运会|运动会)";
		System.out.println(s.split("\\+").length);
		System.out.println(s.replace(")", "").replace("(", "").replace("（", "").replace("）", ""));
		
		
		try {
			System.out.println(getEncodeByHref("http://society.workercn.cn/2/201507/20/150720050212884.shtml","utf-8",null));
		} catch (XPatherException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
